package com.xiaolei.libraryktx

import android.widget.CheckBox
import android.widget.CompoundButton
import com.xiaolei.library.Exts.Ext
import com.xiaolei.library.common.OnSystemCheckedChangeListener

/**
 * 改变选中状态
 * @param isChecked  选中状态
 * @param sysEvent 标示是否是系统调用
 */
inline fun <reified T : CheckBox> T.isChecked(isChecked: Boolean, sysEvent: Boolean = false)
{
    Ext.isChecked(this, isChecked, sysEvent)
}

/**
 * 设置响应事件监听器，用以区分是不是由系统发生的
 */
inline fun <reified T : CheckBox> T.onChecked(crossinline block: (isChecked: Boolean, sysEvent: Boolean) -> Unit)
{
    Ext.setOnChecked(this, object : OnSystemCheckedChangeListener()
    {
        override fun onChanged(buttonView: CompoundButton, isChecked: Boolean, sysEvent: Boolean)
        {
            if (buttonView is T)
            {
                block.invoke(isChecked, sysEvent)
            }
        }
    })
}